//
//  NewHomeBannerCell.swift
//  d11
//
//  Created by peale on 2018/11/16.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias BtnClickBlock = (_ type: Int,_ sender:UIButton) -> Void
typealias BannerClickBlock = (_ data:[String:Any]) -> Void
typealias MarqueeClickBlock = (_ index: Int) -> Void
class NewHomeBannerCell: UITableViewCell,FSPagerViewDataSource,FSPagerViewDelegate,JJMarqueeViewDelegate,JJMarqueeViewDataSource   {
    var btnClickBlock: BtnClickBlock?
    func callChangeBlock(_ block: @escaping BtnClickBlock) {
        
        btnClickBlock = block
    }
    var bannerClickBlock: BannerClickBlock?
    func callClickBlock(_ block: @escaping BannerClickBlock) {
        
        bannerClickBlock = block
    }
     var marqueeClickBlock: MarqueeClickBlock?
    func callMarqueeBlock(_ block: @escaping MarqueeClickBlock) {
        
        marqueeClickBlock = block
    }
    var pagerView : FSPagerView?
    var bannerList : [[String:Any]]?
    var pageControl : FSPageControl?

 //   var blackImageView:UIImageView?

    var bgImageView : UIImageView?
    var bannerBgImageView : UIImageView?
    var iconImageView : UIImageView?
     var lineYView : UIView?
    var moreBtn :UIButton?
    var marqueeView : JJMarqueeView?
     var lineXView : UIView?
    
    
    var gridBtn1 : UIButton?
    var gridImgView1: UIImageView?
    var gridLabel1: UILabel?
    
    var gridBtn2 : UIButton?
    var gridImgView2: UIImageView?
    var gridLabel2: UILabel?
    
    var gridBtn3 : UIButton?
    var gridImgView3: UIImageView?
    var gridLabel3: UILabel?
    
    var gridBtn4 : UIButton?
    var gridImgView4: UIImageView?
    var gridLabel4: UILabel?
    
    var arrayList :[String]?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = HexRGBAlpha(0xf4f7f9, 1)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.bannerList = []
         self.arrayList = []
        self.bannerBgImageView = UIImageView.init(frame: self.bounds)
        self.bannerBgImageView?.contentMode = .scaleAspectFit
       self.bannerBgImageView?.clipsToBounds = true
        self.bannerBgImageView?.image = imageNamed("img_default_banner")
        self.bannerBgImageView?.backgroundColor  = HexRGBAlpha(0xf4f7f9, 1)
        self.contentView.addSubview(self.bannerBgImageView!)
        self.bannerBgImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(self.bounds.width*0.97)
            make.height.equalTo(150)
            make.centerX.equalTo(self.contentView)
        }
        
        self.pagerView = FSPagerView.init(frame: self.bounds)
        self.pagerView?.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")

        self.pagerView?.itemSize = CGSize.zero
        self.pagerView?.interitemSpacing = 10 //间距
        self.pagerView?.automaticSlidingInterval = 5 //间隔时间
        self.pagerView?.isInfinite = true //循环滚动
        self.pagerView?.dataSource = self
        self.pagerView?.delegate = self
//        self.pagerView?.transformer = FSPagerViewTransformer(type:.depth)
        // let transform = CGAffineTransform(scaleX: 0.99, y: 0.99)
        // self.pagerView?.itemSize = (self.pagerView?.frame.size.applying(transform))!
        self.contentView.addSubview(pagerView!)
        self.pagerView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(130)
            make.centerX.equalTo(self.contentView)
        }
        
        self.pageControl = FSPageControl.init(frame: self.bounds)
        self.pageControl?.numberOfPages = self.bannerList!.count
        self.pageControl?.contentHorizontalAlignment = .right
       // self.pageControl?.contentInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        self.pageControl?.hidesForSinglePage = true
        self.pageControl?.contentHorizontalAlignment = .center
        self.pageControl?.interitemSpacing = 10
         self.pageControl?.currentPage = 0
         self.pageControl?.setFillColor(HexRGBAlpha(0xffc569, 1), for: .selected)
        self.pageControl?.setImage(UIImage(named:"icon_pageControl_nol"), for: .normal)
        self.pageControl?.setImage(UIImage(named:"icon_pageControl_sel"), for: .selected)
        self.contentView.addSubview(pageControl!)
        self.pageControl?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.bottom.equalTo((self.pagerView?.snp.bottom)!).offset(5)
        }
//        self.blackImageView = UIImageView.init()
//         self.blackImageView?.backgroundColor = UIColor.black
//         self.contentView.addSubview(self.blackImageView!)
//        self.blackImageView?.snp.makeConstraints{(make) in
//            make.width.equalToSuperview()
//            make.height.equalTo(15)
//            make.centerX.equalToSuperview()
//            make.top.equalTo((self.pagerView?.snp.bottom)!)
//        }
        self.bgImageView = UIImageView.init()
        self.bgImageView?.image = imageNamed("icon_home_top_bg")?
            .resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 30, bottom: 5, right: 30),
                            resizingMode: .stretch)
        self.bgImageView?.isUserInteractionEnabled = true
        self.contentView.addSubview(self.bgImageView!)
        self.bgImageView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-20)
            make.height.equalTo(131)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.pagerView?.snp.bottom)!).offset(2)
        }
        
        self.iconImageView = UIImageView.init(image: UIImage.init(named: "icon_home_tt2"))
        self.bgImageView?.addSubview(self.iconImageView!)
        iconImageView?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(12)
            make.width.equalTo(63)
            make.height.equalTo(14.5)
            make.top.equalToSuperview().offset(12)
        }
        
        self.lineYView = UIView.init()
        self.lineYView?.backgroundColor = HexRGBAlpha(0xffe2b4, 1)
        self.bgImageView?.addSubview(self.lineYView!)
        lineYView?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImageView?.snp.right)!).offset(3)
            make.width.equalTo(1)
            make.height.equalTo(16.5)
            make.top.equalToSuperview().offset(12)
        }
        
        self.moreBtn = UIButton.init(type: .custom)
        self.moreBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.moreBtn?.setTitle("更多 >>", for: .normal)
        self.moreBtn?.setTitleColor(HexRGBAlpha(0xffc569, 1), for: .normal)
        self.moreBtn?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.moreBtn?.tag = 1000
        self.bgImageView?.addSubview(self.moreBtn!)
        moreBtn?.snp.makeConstraints{(make) in
            make.right.equalToSuperview().offset(-6)
            make.width.equalTo(65)
            make.height.equalTo(30)
            make.top.equalToSuperview().offset(5)
        }
        self.marqueeView = JJMarqueeView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth-97-5-70, height:30.0))
         self.marqueeView?.automaticSlidingInterval = 3
         self.marqueeView?.delegate = self
         self.marqueeView?.dataSource = self
        self.bgImageView?.addSubview( self.marqueeView!)
  //       self.carouselView?.setCarouselData(carouselData: titleArr as [AnyObject])
         self.marqueeView?.snp.makeConstraints{(make) in
            make.left.equalTo((self.lineYView?.snp.right)!).offset(2)
            make.right.equalTo((self.moreBtn?.snp.left)!).offset(-2)
            make.height.equalTo(30)
            make.top.equalToSuperview().offset(5)
        }
        
        self.lineXView = UIView.init()
        self.lineXView?.backgroundColor = HexRGBAlpha(0xffe2b4, 1)
        self.bgImageView?.addSubview(self.lineXView!)
        lineXView?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImageView?.snp.left)!)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(1)
            make.top.equalTo((self.marqueeView?.snp.bottom)!)
        }
        
        self.gridBtn1 = UIButton.init(type: .custom)
        self.gridBtn1?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn1?.tag = 1001
        self.bgImageView?.addSubview(self.gridBtn1!)
        self.gridBtn1?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth-50)/4)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(65)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(10)
        }
        
        self.gridImgView1 = UIImageView.init(image: imageNamed("icon_home_ck"))
        self.gridBtn1?.addSubview(self.gridImgView1!)
        self.gridImgView1?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
             make.top.equalTo(2)
        }
        
        
        self.gridLabel1 = UILabel.init()
        self.gridLabel1?.font = UIFont.systemFont(ofSize: 14)
        gridLabel1?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel1?.textAlignment = .center
        gridLabel1?.text = "存款"
        self.gridBtn1?.addSubview(self.gridLabel1!)
        self.gridLabel1?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView1?.snp.bottom)!)
        }
        
        self.gridBtn2 = UIButton.init(type: .custom)
        self.gridBtn2?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn2?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn2?.tag = 1002
         self.bgImageView?.addSubview(self.gridBtn2!)
        self.gridBtn2?.snp.makeConstraints{(make) in
              make.width.equalTo((kScreenWidth-50)/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn1?.snp.right)!)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(10)
        }
        
        self.gridImgView2 = UIImageView.init(image: imageNamed("icon_home_qk"))
        self.gridBtn2?.addSubview(self.gridImgView2!)
        self.gridImgView2?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel2 = UILabel.init()
        self.gridLabel2?.font = UIFont.systemFont(ofSize: 14)
        gridLabel2?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel2?.textAlignment = .center
         gridLabel2?.text = "取款"
        self.gridBtn2?.addSubview(self.gridLabel2!)
        self.gridLabel2?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView2?.snp.bottom)!)
        }
        
        self.gridBtn3 = UIButton.init(type: .custom)
        self.gridBtn3?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn3?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn3?.tag = 1003
         self.bgImageView?.addSubview(self.gridBtn3!)
        self.gridBtn3?.snp.makeConstraints{(make) in
              make.width.equalTo((kScreenWidth-50)/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn2?.snp.right)!)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(10)
        }
        self.gridImgView3 = UIImageView.init(image: imageNamed("icon_home_yh"))
        self.gridBtn3?.addSubview(self.gridImgView3!)
        self.gridImgView3?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
             make.top.equalTo(2)
        }
        
        
        self.gridLabel3 = UILabel.init()
        self.gridLabel3?.font = UIFont.systemFont(ofSize: 14)
        gridLabel3?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel3?.textAlignment = .center
        gridLabel3?.text = "优惠"
        self.gridBtn3?.addSubview(self.gridLabel3!)
        self.gridLabel3?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView3?.snp.bottom)!)
        }
        
        self.gridBtn4 = UIButton.init(type: .custom)
        self.gridBtn4?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn4?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn4?.tag = 1004
         self.bgImageView?.addSubview(self.gridBtn4!)
        self.gridBtn4?.snp.makeConstraints{(make) in
            make.width.equalTo((kScreenWidth-50)/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn3?.snp.right)!)
            make.top.equalTo((lineXView?.snp.bottom)!).offset(10)
        }
        self.gridImgView4 = UIImageView.init(image: imageNamed("icon_home_zd"))
        self.gridBtn4?.addSubview(self.gridImgView4!)
        self.gridImgView4?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
             make.top.equalTo(2)
        }
        
        
        self.gridLabel4 = UILabel.init()
        self.gridLabel4?.font = UIFont.systemFont(ofSize: 14)
        gridLabel4?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel4?.textAlignment = .center
        gridLabel4?.text = "投注记录"
        self.gridBtn4?.addSubview(self.gridLabel4!)
        self.gridLabel4?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView4?.snp.bottom)!)
        }
    }
    
    func setData(bannerList:[[String:Any]]? ,arrayList:[String]?) -> Void {
        self.bannerList = bannerList
        self.arrayList = arrayList
        self.marqueeView?.reload()
        self.pageControl?.numberOfPages = self.bannerList?.count ?? 0
        self.pageControl?.currentPage = self.pagerView?.currentIndex ?? 0
        self.pagerView?.reloadData()
        if self.pageControl?.numberOfPages == 0{
            self.bannerBgImageView?.isHidden = false
        }else{
            self.bannerBgImageView?.isHidden = true
        }
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        
        let banner = self.bannerList![index] as? [String:Any] ?? [:]
        cell.imageView?.sd_setImage(with: URL.init(string: banner["imgUrl"] as? String ?? ""), placeholderImage: imageNamed("img_default_banner"), options: .refreshCached, completed: nil);

        cell.imageView?.contentMode = .scaleToFill
        cell.imageView?.clipsToBounds = true
        
        return cell
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.bannerList?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        let banner = self.bannerList![index] as? [String:Any] ?? [:]
        print("\(banner)")
        self.pageControl?.currentPage = index
        if bannerClickBlock != nil{
              bannerClickBlock!(banner)
        }
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
        guard self.pageControl?.currentPage != pagerView.currentIndex else {
            return
        }
        self.pageControl?.currentPage = pagerView.currentIndex // Or Use KVO with property "currentIndex"
    }
    
    
    
    
    
    
    
    func numberOfItems(_ marqueeView: JJMarqueeView) -> Int {
        return arrayList?.count ?? 0
    }
    func marqueeView(_ marqueeView: JJMarqueeView, cellForItemAt index: Int) -> NSAttributedString {
//        print(index)
        let att = NSMutableAttributedString.init(string: arrayList?[index] ?? "")
        return att
    
    }
   func mqrqueeView(_ marqueeView:JJMarqueeView,didSelectCellAt index:Int) -> Void
    {
//        print("!!!!!!!\(index)")
        if marqueeClickBlock != nil {
            marqueeClickBlock!(index)
        }
    }
    @objc func actionToClick(_ sender:UIButton) -> Void {
        if btnClickBlock != nil{
            btnClickBlock!(sender.tag,sender)
        }
    }
}
